# -*- coding: utf-8 -*-
import json

from fastapi import Request

from configs.base_config import JOB_COMPANY_LIST_FILE
from schemas.response_entity import JsonModel


async def get_job_list(request: Request):
    user = request.state.user
    if user is None:
        return JsonModel(code=401, data=None, msg="用户token非法").to_response()

    with open(JOB_COMPANY_LIST_FILE, "r", encoding='utf-8') as f:
        content = f.read()
        if content.strip() == "":
            # 如果文件为空，初始化为空列表
            all_data = []
        else:
            all_data = json.loads(content)

    job_list = all_data.get("job_list", [])
    return JsonModel(code=200, data={"job_list": job_list}, msg="success").to_response()


async def get_company_list(request: Request):
    user = request.state.user
    if user is None:
        return JsonModel(code=401, data=None, msg="用户token非法").to_response()

    with open(JOB_COMPANY_LIST_FILE, "r", encoding='utf-8') as f:
        content = f.read()
        if content.strip() == "":
            # 如果文件为空，初始化为空列表
            all_data = []
        else:
            all_data = json.loads(content)

    company_list = all_data.get("company_list", [])
    return JsonModel(code=200, data={"company_list": company_list}, msg="success").to_response()
